/**
 * 
 *  @project Air Traff Monitor
 * 
 *  @author M.Emre ATASEVER <m.emre.atas@gmail.com>
 *  @author M.Burak ALKAN <mburakalkan@gmail.com>
 *  @author H.Burak KARAMIK <bkaramik@gmail.com>
 *
 *	`Air Traffic Monitor' is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "alertview.h"

#include <QHeaderView>
#include <QApplication>
#include <QTime>

#include <QToolBar>

QKismetAlertWnd::QKismetAlertWnd(QKismetSocket *sock)
{
	tree	= new QKismetAlertView(sock);
	toolbar	= new QKismetViewToolBar(tree);

	tree->popupMenu->addSeparator();
	tree->popupMenu->addAction(toolbar->toggleViewAction());
	
	setCentralWidget(tree);
	addToolBar(toolbar);
}
QKismetAlertWnd::~QKismetAlertWnd()
{

	delete tree;
	delete toolbar;
}

QKismetAlertDock::QKismetAlertDock(QKismetSocket *sock)
{
	hide();
	
	wnd	= new QKismetAlertWnd(sock);

	QAction *a = toggleViewAction();
	a->setIcon(QIcon(":/img/emblem-important.png"));
	a->setStatusTip("Toggle alerts window");
	setWindowTitle("Alerts");

	setWidget(wnd);
}
QKismetAlertDock::~QKismetAlertDock()
{
	delete wnd;
}

QKismetAlertView::QKismetAlertView(QKismetSocket *sock) : QKismetTreeView(new AlertModel(sock))
{
	socket = sock;
	
	connect(this,SIGNAL(setOption(QString,bool)),socket,SLOT(SetAlertOption(QString,bool)));	
	
	setRootIsDecorated(false);
	createMenus();
}

QKismetAlertView::~QKismetAlertView()
{
	SaveSettings();
}

void QKismetAlertView::createMenus()
{

	headMenu = appendMenu("Network info",socket->alert.getNames(),socket->alert.getCaps(),0,0,9);
	QList<QAction*> acts = headMenu->actions();
	acts[0]->setEnabled(false);
	
	QHeaderView *head = header();

	head->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(head,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(showHeadMenu(const QPoint&)));

	QString ss("AlertviewOptions");
	RestoreSettings(ss,acts,QList<QVariant>() <<0<<3<<8);
}

void QKismetAlertView::SaveSettings()
{
	QString ss("AlertviewOptions");
	WriteSettings(ss);
}
